package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Cmf_Integral;
import cn.wolfcode.trip.base.domain.Like;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.Cmf_IntegralMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private Cmf_IntegralMapper integralMapper;

    @Autowired
    private ITravelService travelService;

    @PostMapping
    @ResponseBody
    public Object register(User user){
        JSONResult result = new JSONResult();
        try{
        User register = userService.register(user);

        }catch (Exception e){
            result.mark(e.getMessage());
        }
        return result;
    }

    /**
     * 根据ID获取指定用户
     * @param id
     * @return
     */

    @GetMapping("/{id}/user")
    @ResponseBody
    public Object getUser(@PathVariable Long id){
        User user=userService.getUserById(id);
        return user;
    }
    //checkLogin
    @GetMapping(value = "/check")
    @ResponseBody
    public Object check(User user){
        try{
            User register = userService.register(user);
        }catch (Exception e){
            return false;
        }
        return true;
    }



    //更新用户操作
    @PutMapping
    @ResponseBody
    public Object update(User user){
        JSONResult json = new JSONResult();

        try {
            User userUpdate = userService.update(user);
            //把更新后的数据设置
            UserContext.setCurrentUser(userUpdate);
            json.setData(userUpdate);
        }catch (Exception e){
            e.printStackTrace();
            json.mark(e.getMessage());
        }
        return json;
    }


    //获取我的文章
    @GetMapping(value = "/{id}/travel")
    @ResponseBody
    public Object myTravel(@PathVariable Long id, TravelQueryObject qo){
        User u = new User();
        u.setId(id);
        qo.setAuthor(u);
        if (qo.getState()==null){
            qo.setState(-1);
        }
        PageInfo list = travelService.getList(qo);

        return list;
    }

    @GetMapping(value = "/{id}")
    @ResponseBody
    public Object myMessqge(@PathVariable Long id){

        return userService.get(id);
    }

    //更改粉丝
    @PutMapping(value = "/{id}/author")
    @ResponseBody
    public Object putFans(@PathVariable Long id){
        JSONResult result = new JSONResult();
        try{
            userService.putFans(id);
        } catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    //查看粉丝
    @GetMapping(value = "/{id}/fans")
    @ResponseBody
    public Object getFans(@PathVariable Long id){

        return userService.getFans(id);
    }

    //查看粉丝
    @GetMapping(value = "/{id}/attention")
    @ResponseBody
    public Object getAttention(@PathVariable Long id){

        return userService.getAttention(id);
    }


    //获取我的点赞信息
    @GetMapping(value = "/{id}/like")
    @ResponseBody
    public Object myLike(@PathVariable Long id){
           List<Like> likes= userService.selectLikeByAuthorId(id);
        return likes;

    }
    //获取我的未读点赞信息
    @GetMapping("/{id}/getNoReadLike")
    @ResponseBody
    public Object getNoReadLike(@PathVariable Long id){
        int likes = userService.getNoReadLikeByAuthorId(id);

        return likes;
    }

    //更新点赞已读
    @PostMapping("/{id}/setLikeHasRead")
    @ResponseBody
    public Object setLikeHasRead(@PathVariable Long id){
        userService.setLikeHasReadByAuthorId(id);
        return "";
    }

    //获取当前用户的积分
    @ResponseBody
    @GetMapping("/{userId}/integral")
    public Object MyIntegral(@PathVariable Long userId){
        Cmf_Integral cmf_integral = integralMapper.selectByPrimaryKey(userId);
        return cmf_integral;
    }

}
